package com.neuedu.springmvc.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回ModelAndview
 */
@Controller
public class UserController {


    /**
     * 返回ModelAndView
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(){

        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        modelAndView.addObject("list",list);
        modelAndView.setViewName("user/user_list");
        return modelAndView;
        
    }



    /**
     * http://localhost:8080/springmvc06/returnvoid
     *   HTTP状态 404 - 未找到  /WEB-INF/jsp/returnvoid.jsp
     * 默认 方法名作为视图的名字
     * 返回ModelAndView
     * @return
     */
    @RequestMapping("/returnvoid")
    public void returnvoid(){

        System.out.println("UserController.returnvoid");
    }


    /**
     * 自定义响应内容，不需要 html 模板
     * @param response
     * @throws IOException
     */
    @RequestMapping("/returnvoidByResponse")
    public void returnvoidByResponse(HttpServletResponse response) throws IOException {

        System.out.println("UserController.returnvoidByResponse");
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.write("{name:'abc',age:18}");
        out.flush();
        out.close();
    }


    /**
     * String 返回String，默认当做视图名称
     * modelMap 相当于ModelAndView中 Model，设置共享数据给视图去渲染
     * @return
     */
    @RequestMapping("/req1")
    public String req1(ModelMap modelMap){


        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        modelMap.addAttribute("list",list);


        return "user/user_list";

    }


    /**
     * String 返回String，默认当做视图名称
     *    forward: 请求转发  相当于脱离视图解析器配置的前缀和后缀的管理
     * modelMap 相当于ModelAndView中 Model，设置共享数据给视图去渲染
     * @return
     */
    @RequestMapping("/req2")
    public String req2(ModelMap modelMap,HttpServletRequest req){


        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        modelMap.addAttribute("list",list);

        //req.getRequestDispatcher("/").forward(req,resposne);

        return "forward:/other/user_list_other.jsp";

    }



    /**
     * String 返回String，默认当做视图名称
     *    redirect: 重定向
     * modelMap 相当于ModelAndView中 Model，设置共享数据给视图去渲染
     * @return
     */
    @RequestMapping("/req3")
    public String req3(){

        //response.sendRedirect(req.getContextPath()+"/index.jsp");
        // 重定向
        return "redirect:index.jsp";

    }






}
